//
//  OAuthViewController.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/3.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "OAuthViewController.h"
#import "UserAccount.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "UserAccountViewModel.h"

@interface OAuthViewController () <UIWebViewDelegate>
@property (nonatomic, strong)UIWebView *webView;
@end


@implementation OAuthViewController

- (void)close {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)fullAccout {
    
    NSString *jsString = @"document.getElementById('userId').value = '18600846045' , document.getElementById('passwd').value = '99475670258zkY';";
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
}

- (void)loadView {
    self.webView = [[UIWebView alloc] init];
    self.view = self.webView;
    self.webView.delegate = self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];

    // 加载网页
    [self loadOauthPage];
}

- (void)setupUI {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(fullAccout)];
    
}

- (void)loadOauthPage {
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@", client_id, redirect_uri];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)viewDidDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}


#pragma mark -- 专门处理 webView所有的协议方法

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD show];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}

 // 协议方法 如果返回 bool  一般表示 如果为true  表示当前对象可以正常 工作  否则就不能
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *urlString = request.URL.absoluteString;
    if ([urlString hasPrefix:@"https://api.weibo.com"]) {
        return true;
    }
    
    if (![urlString hasPrefix:redirect_uri]) {
        return false;
    }
    
    // 程序走到这里  一定包含了  回调的url
    NSString *query = request.URL.query;
    NSString *codeStr = @"code=";
    NSString *code = [query substringFromIndex:codeStr.length];
    
    UserAccountViewModel *userAccViewModel = [[UserAccountViewModel alloc] init];
    
    [userAccViewModel loadAccessToken:code WithFinished:^(NSError *error) {
        
        if (error != nil) {
            [SVProgressHUD showInfoWithStatus:AppErrorTip];
            return ;
        }
        // modal出来的页面 被dismiss掉 不会立即被回收
        // modal出来的页面 要注意dismiss
        [self dismissViewControllerAnimated:false completion:^{
            // 发出页面切换的通知
            // moadl出来的页面  需要立即切换页面并且dismiss  需要在dismiss完成后的闭包中 开始页面的切换
            [[NSNotificationCenter defaultCenter] postNotificationName:AppSwitchRootViewControler object:@"Welcome"];
            // 通知是同步的
        }];
//        [[NSNotificationCenter defaultCenter] postNotificationName:AppSwitchRootViewControler object:@"Welcome"];
//        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    return false;
}
@end

